﻿using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using LogLib;
using System.Data;
using System.Collections.Generic;
using ComLib.ComFun;
using ComLib.ConfigService;
using ComLib.ComType;
using ProcessConfig.ConfigType;
using ProcessConfig;
using DetectProcessing;

namespace KProcessor
{
    public class InitComponents
    {       
        private string _strErrInfo = "";
        public string strErrInfo
        {
            get { return _strErrInfo; }
        }


        private bool _bInitQCLib = false;

        private bool _bInitPxlLib = false;

        private bool _bInitBufferPool = false;

        private const string PrinterName = "PhotoPrint";

        private PixelDetectFactory PxlFactory = PixelDetectFactory.Instance;

        private BufferManage _bufferManage;

        public InitComponents()
        {
          
        }

        private  ConfigService _service;
        public ConfigService Service
        {
            get { return _service; }
        }
   
        private void InitConfigData()
        {
            _strErrInfo = "";
            try
            {
                _service = ConfigService.GetInstance();
                _service.LoadConfig();
            }
            catch (Exception ex)
            {
                _strErrInfo = ex.Message;
                Log.WriteException(ex.ToString());
            }
        }

        delegate void CheckDelegate(string s);
        public static void CheckFolder()
        {
            CheckDelegate delFun = (string path) =>
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            };
            //delFun(Configs.LogUsage.UserRecordPath);
        }

        private void InitConfigService()
        {
            InitConfigData();
            CheckFolder();
            if (!_service.CheckLoadAllSuccess())
            {
                string[] errArray = _service.GetLoadErrors();
                string title = "部分参数加载异常，详细异常信息如下:";
                Console.WriteLine(title);
                Log.WriteAsync(title);
                foreach (string err in errArray)
                {
                    Console.WriteLine(err);
                }
            }
            Log.WriteAsync("系统参数加载完成.");
        }
      

        public bool Init()
        {
            Log.AddBeginningBoundary();          
            _strErrInfo = string.Empty;       
            InitConfigService();
            if (PxlFactory.Init())
            {
                _bInitPxlLib = true;
            }
            else
            {
                _bInitPxlLib = false;
                _strErrInfo = PxlFactory.ErrorMessage;
                Log.WriteAsync("PXL类库初始化失败:" + _strErrInfo);
                return false;
            }
         
            if (!CreateBufferManage())
            {
                return false;
            }
            return true;
        }

        public void Close()
        {
            if (!_bInitBufferPool)
            {
                RelesseBuffer();
            }     
            if(_bInitPxlLib)
            {
                PxlFactory.Dispose();
                Log.WriteAsync("PXL 句柄已释放.");
            }
          
            Log.AddEndingBoundary();
        }

      
        public bool CreateBufferManage()
        {
            BufferManage.CreatInstance();
            _bufferManage = BufferManage.Instance;
            if (_bufferManage.CreatPool(32))
            {
                _bInitBufferPool = true;
                Log.WriteAsync("内存缓冲区已经创建...");
                return true;
            }
            else
            {
                _strErrInfo = "缓冲区创建失败:" + _bufferManage.ErrorMessage;
                Log.WriteAsync(_strErrInfo);
                return false;
            }
        }

        private void RelesseBuffer()
        {
            BufferManage.ReleaseInstance();
        }

        /// <summary>
        /// 检查参数配置的合法性
        /// </summary>
        /// <returns></returns>
        //private bool CheckParasValidity()
        //{
        //    if (!Configs.CamOption.CheckValidRect(Configs.PhoSize.CameraSize))
        //    {
        //        _strErrInfo = Configs.CamOption.ErrorMessage;
        //        return false;
        //    }
        //    return true;
        //}
    }
}
